{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "8aa7b7e0-38eb-4625-8aa8-373f5b637012",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "%goflags=[\"--cover\" \"--covermode=set\"]\n"
     ]
    }
   ],
   "source": [
    "%goflags --cover --covermode=set"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "83137df3-2c1a-4bd6-802d-0e181372bc04",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "55\n",
      "2178309\n",
      "2178309\n"
     ]
    }
   ],
   "source": [
    "import (\n",
    "    \"math\"\n",
    "    \"golang.org/x/exp/constraints\"\n",
    ")\n",
    "\n",
    "// Slow\n",
    "func FibonacciA(n int) int {\n",
    "    if n <= 1 {\n",
    "        return n\n",
    "    }\n",
    "    return FibonacciA(n-1) + FibonacciA(n-2)    \n",
    "}\n",
    "\n",
    "// Fast\n",
    "func FibonacciB(n int) int {\n",
    "    if n <= 1 {\n",
    "        return n\n",
    "    }\n",
    "    fib, fibMinus1 := 1, 0\n",
    "    for ii := 2; ii <= n; ii ++ {\n",
    "        newFib := fib + fibMinus1\n",
    "        fib, fibMinus1 = newFib, fib\n",
    "    }\n",
    "    return fib\n",
    "}\n",
    "\n",
    "%%\n",
    "fmt.Println(FibonacciA(10))\n",
    "fmt.Println(FibonacciA(32))\n",
    "fmt.Println(FibonacciB(32))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "24e43ada-add1-41dd-b8d3-3c04a7a2b01c",
   "metadata": {},
   "source": [
    "#### Test is correctly run, `go get -t` is used to fetch imports"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "ab7309e8-7c0e-4d78-b664-01c2fb737fc5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "=== RUN   TestA\n",
      "Testing A\n",
      "--- PASS: TestA (0.00s)\n",
      "PASS\n",
      "coverage: [no statements]\n"
     ]
    }
   ],
   "source": [
    "%test\n",
    "import (\n",
    "\t\"github.com/stretchr/testify/require\"\n",
    "    \"testing\"\n",
    ")\n",
    "\n",
    "func TestA(t *testing.T) {\n",
    "    fmt.Printf(\"Testing A\\n\")\n",
    "    require.Equal(t, 55, FibonacciA(10))\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4145340b-60f9-402a-8cea-37776216e339",
   "metadata": {},
   "source": [
    "#### Cells with `%test` should also have its functions memorized"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "27cb0775-c925-4f5d-8213-9df896f1ffd4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<h3>Memorized Definitions</h3>\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<h4>Imports</h4>\n",
       "<ul>\n",
       "<li><pre>constraints</pre></li>\n",
       "<li><pre>math</pre></li>\n",
       "<li><pre>require</pre></li>\n",
       "<li><pre>testing</pre></li>\n",
       "</ul>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<h4>Functions</h4>\n",
       "<ul>\n",
       "<li><pre>FibonacciA</pre></li>\n",
       "<li><pre>FibonacciB</pre></li>\n",
       "<li><pre>TestA</pre></li>\n",
       "</ul>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%ls"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "12f29ab0-6021-420c-a54a-01fa7f57bca6",
   "metadata": {},
   "source": [
    "#### If `%test` is executed alone, it runs all tests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "3a0415f9-e118-49f2-a531-d0581ef15f15",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "=== RUN   TestA\n",
      "Testing A\n",
      "--- PASS: TestA (0.00s)\n",
      "PASS\n",
      "coverage: [no statements]\n"
     ]
    }
   ],
   "source": [
    "%test"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "164e92be-f4d5-46ed-bd67-6ee1bc8285b0",
   "metadata": {},
   "source": [
    "#### If `Test*` functions are defined, only they are executed by default"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "a5180470-4076-49cb-a933-62af7309c740",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "=== RUN   TestAB\n",
      "Testing AB\n",
      "--- PASS: TestAB (0.00s)\n",
      "=== RUN   TestB\n",
      "Testing B\n",
      "--- PASS: TestB (0.00s)\n",
      "PASS\n",
      "coverage: [no statements]\n"
     ]
    }
   ],
   "source": [
    "func TestB(t *testing.T) {\n",
    "    fmt.Printf(\"Testing B\\n\")\n",
    "    require.Equal(t, 55, FibonacciB(10))\n",
    "}\n",
    "\n",
    "func TestAB(t *testing.T) {\n",
    "    fmt.Printf(\"Testing AB\\n\")\n",
    "    require.Equal(t, 55, FibonacciA(10))\n",
    "    require.Equal(t, 55, FibonacciB(10))\n",
    "}\n",
    "\n",
    "%test"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9ef23dd2-08ac-4d61-b5b7-58d5a81062f8",
   "metadata": {},
   "source": [
    "#### Passing parameters: it should disable `--test.v` which is enabled by default."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "182e8fe5-ce2f-4a55-9aca-8dabe18cf784",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Testing A\n",
      "Testing AB\n",
      "Testing B\n",
      "PASS\n",
      "coverage: [no statements]\n"
     ]
    }
   ],
   "source": [
    "%test \"\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1b6f7cb4-7c55-48c4-998a-663a969d2e2d",
   "metadata": {},
   "source": [
    "#### Bechmarks"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "22584bfc-1261-432f-afe1-7b9f5919412b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "goos: linux\n",
      "goarch: amd64\n",
      "pkg: gonb_0591052b\n",
      "cpu: AMD Ryzen 9 9950X3D 16-Core Processor          \n",
      "BenchmarkFibonacciA32\n",
      "BenchmarkFibonacciA32-32    \t     192\t   6233978 ns/op\n",
      "BenchmarkFibonacciB32\n",
      "BenchmarkFibonacciB32-32    \t207941161\t         5.898 ns/op\n",
      "PASS\n",
      "coverage: [no statements]\n"
     ]
    }
   ],
   "source": [
    "func BenchmarkFibonacciA32(b *testing.B) {\n",
    "    for i := 0; i < b.N; i++ {\n",
    "        FibonacciA(32)\n",
    "    }\n",
    "}\n",
    "\n",
    "func BenchmarkFibonacciB32(b *testing.B) {\n",
    "    for i := 0; i < b.N; i++ {\n",
    "        FibonacciB(32)\n",
    "    }\n",
    "}\n",
    "\n",
    "%test"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "adc9ec07-e41d-41aa-bf5a-550a99a16847",
   "metadata": {},
   "source": [
    "#### To run all benchmarks in notebook"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "49177bc7-1486-4170-915e-8f8b48cc3d2e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "goos: linux\n",
      "goarch: amd64\n",
      "pkg: gonb_0591052b\n",
      "cpu: AMD Ryzen 9 9950X3D 16-Core Processor          \n",
      "BenchmarkFibonacciA32-32    \t     192\t   6382527 ns/op\n",
      "BenchmarkFibonacciB32-32    \t207262278\t         5.794 ns/op\n",
      "PASS\n",
      "coverage: [no statements]\n"
     ]
    }
   ],
   "source": [
    "%test -test.bench=. -test.run=Bechmark"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Go (gonb)",
   "language": "go",
   "name": "gonb"
  },
  "language_info": {
   "codemirror_mode": "",
   "file_extension": ".go",
   "mimetype": "text/x-go",
   "name": "go",
   "nbconvert_exporter": "",
   "pygments_lexer": "",
   "version": "go1.25.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
